﻿unit ChangeCollectorFrm;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  cxGridExportLink,
  dxRibbonForm,
  FIBQuery, pFIBCacheQueries,
  Dialogs, dxSkinsCore, dxSkinsdxBarPainter, dxSkinMcSkin, dxSkinSeven,
  cxGraphics, cxControls, cxLookAndFeels, cxLookAndFeelPainters,
  dxSkinsdxRibbonPainter, cxButtonEdit, dxBar, cxBarEditItem, cxClasses,
  dxRibbon, cxDropDownEdit, JvDialogs, dxSkinsdxStatusBarPainter, cxStyles,
  dxSkinscxPCPainter, cxCustomData, cxFilter, cxData, cxDataStorage, cxEdit, DB,
  cxDBData, MemTableDataEh, MemTableEh, cxGridLevel, cxGridCustomView,
  cxGridCustomTableView, cxGridTableView, cxGridDBTableView, cxGrid, dxStatusBar,
  FIBDataSet, pFIBDataSet, dxBarExtDBItems, cxDBLookupComboBox, Menus, StdCtrls,
  cxButtons, cxContainer, cxTextEdit, cxMaskEdit, cxLookupEdit, cxDBLookupEdit,
  cxSplitter, ExtCtrls, cxLabel, dxSkinOffice2010Blue, dxSkinOffice2010Silver,
  dxSkinOffice2010Black;

type
  TChangeCollectorForm = class(Tdxcustomribbonform)
    bar1: TdxBarManager;
    rt: TdxRibbonTab;
    dxRibbon1: TdxRibbon;
    br1: TdxBar;
    dlg1: TJvOpenDialog;
    btn2: TdxBarButton;
    BarScreenTipRepository1: TdxBarScreenTipRepository;
    BarScreenTipRepository1ScreenTip1: TdxBarScreenTip;
    stat1: TdxStatusBar;
    v1: TcxGridDBTableView;
    l1: TcxGridLevel;
    grd1: TcxGrid;
    table1: TMemTableEh;
    ds1: TDataSource;
    clAcCode: TcxGridDBColumn;
    FDS1: TpFIBDataSet;
    ds2: TDataSource;
    pnl1: TPanel;
    split1: TcxSplitter;
    cbb_collector: TcxLookupComboBox;
    btn_chenge: TcxButton;
    lbl1: TcxLabel;
    procedure FormCreate(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormActivate(Sender: TObject);
    procedure FormDeactivate(Sender: TObject);
    procedure btn2Click(Sender: TObject);
    procedure btn_chengeClick(Sender: TObject);
  private
    { Private declarations }
    procedure LoadSchet(fname :string);
    procedure CreateTable;
    procedure Collector_Setup(collector :string);
  public
    { Public declarations }
  end;

var
  ChangeCollectorForm: TChangeCollectorForm;

implementation

uses cmUtils, MainFrm, DModule;

{$R *.dfm}

procedure TChangeCollectorForm.btn2Click(Sender: TObject);
begin
 if dlg1.Execute(Handle) then
 LoadSchet(dlg1.FileName);
end;
{------------------------------------------------------------------------------
LoadSchet:  Загружаем счета из файла
_______________________________________________________________________________}
procedure TChangeCollectorForm.LoadSchet(fname :string);
var
  ls :TStringList;
  i :Integer;
  AcCode :string;
begin
  ls:=TStringList.Create;
  ls.LoadFromFile(fname);
  if not table1.IsEmpty then  table1.EmptyTable;

  v1.DataController.DataSource:=nil;
  for I := 0 to Ls.Count - 1 do
  begin
    AcCode:=Trim(ls[i]);
    if AcCode<>'' then
    begin
      table1.Append;
      if Length(AcCode)=8 then AcCode:='0'+AcCode;
      table1.FieldByName('AcCode').AsString:=AcCode;
      table1.Post;
    end;
  end;
  v1.DataController.DataSource:=ds1;
  ls.Free;
  if not table1.IsEmpty then
  begin
    cbb_collector.Enabled:=True;
    btn_chenge.Enabled:=True;
  end;
end;

procedure TChangeCollectorForm.btn_chengeClick(Sender: TObject);
var
  col :string;
begin
 //устанавливаем счета на коллектора
 col:= cbb_collector.Properties.Grid.DataController.Values
  [cbb_collector.Properties.Grid.FocusedRowIndex,0];
  if col<>'' then  Collector_Setup(col);
end;
{-------------------------------------------------------------------------------
Collector_Setup | устанавливаем коллектора
-------------------------------------------------------------------------------}
procedure TChangeCollectorForm.Collector_Setup(collector :string);
var
  ls :TStringList;
  Q,Q1 :TfibQuery;
begin
  ls:=TStringList.Create;
  SetWaitCursor(true);
  if dm1.trnWr.Active=false then dm1.trnWr.Active:=True;
  Q:=GetQueryForUse(dm1.trnRD,
                    'select Accode from n_reestrData where accode=:accode');
   v1.DataController.DataSource:=nil;
   table1.First;
   try
   while not table1.Eof do
   begin
     Q.ParamByName('accode').AsString:=table1.FieldByName('AcCode').AsString;
     Q.ExecQuery;
     if Q.FN('AcCode').AsString<>'' then
     begin
       Q1:=GetQueryForUse(dm1.trnWr,
                             'execute procedure CHANGE_COLLECTOR(:Accode ,:Usr)');
       Q1.ParamByName('Accode').AsString:=table1.FieldByName('AcCode').AsString;
       Q1.ParamByName('Usr').AsInteger:=StrToInt(collector);
       Q1.ExecQuery;
       stat1.Panels[0].Text:=IntToStr(table1.RecNo);
       stat1.Panels[1].Text:='обновляем реестр ...';
     end;
     Application.ProcessMessages;
     table1.Next;
   end;
   dm1.trnWr.Commit;
   v1.DataController.DataSource:=ds1;
   finally
     SetWaitCursor(false);
   end;
   ShowMessage('Записи обновленны.');
   stat1.Panels[1].Text:='Записи обновленны.';
  ls.Free;
  FreeQueryForUse(Q);
  FreeQueryForUse(Q1);
  Close;
end;
{------------------------------------------------------------------------------
CreateTable:  Создаем времменую таблицу
_______________________________________________________________________________}
procedure TChangeCollectorForm.CreateTable;
begin
  with table1.FieldDefs do
  begin
    Add('AcCode',ftString,32,True);

  end;
  table1.CreateDataSet;
  table1.Open;
end;

procedure TChangeCollectorForm.FormActivate(Sender: TObject);
begin
   SetTabActive('spr',True);
   MainForm.br_collChange.Visible:=true;
end;

procedure TChangeCollectorForm.FormClose(Sender: TObject;
  var Action: TCloseAction);
begin
  RemoveBtn(Name);
  Action:=caFree;
end;

procedure TChangeCollectorForm.FormCreate(Sender: TObject);
begin
  AddButton(self);
  CreateTable;
  FDS1.CloseOpen(true);
end;

procedure TChangeCollectorForm.FormDeactivate(Sender: TObject);
begin
  MainForm.br_collChange.Visible:=false;
end;

end.
